package com.genntii.examManager.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.genntii.examManager.domain.entity.Course;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.ArrayList;

@Mapper
public interface CourseMapper extends BaseMapper<Course> {

    @Select("select * from course where id = #{courseId}")
    Integer courseExists(@Param("courseId") Long courseId);

    @Select("select * from course order by update_time DESC limit #{pageNum},#{pageSize}")
    ArrayList<Course> getCourseListLatest(@Param("pageNum") Integer pageNum,
                                    @Param("pageSize") Integer pageSize);

    @Select("select * from course limit #{pageNum}, #{pageSize}")
    ArrayList<Course> getCourseList(@Param("pageNum")Integer pageNum,
                                    @Param("pageSize")Integer pageSize);

    @Select("select count(*) from course")
    long getCourseTotal();

    @Select("select *from course where course_name like concat('%',#{keyword},'%') " +
            "order by update_time DESC limit #{pageNum},#{pageSize}")
    ArrayList<Course> getCourseListByKeyword(@Param("keyword")String keyword,
                                            @Param("pageNum")Integer pageNum,
                                            @Param("pageSize")Integer pageSize);

    @Select("select count(*) from course where course_name like concat('%',#{keyword},'%')")
    long getCourseTotalByKeyword(@Param("keyword")String keyword);

    @Select("select * from course where id = #{courseId}")
    Course getCourseById(@Param("courseId") Long courseId);

    ArrayList<Course> getCourseBatch(ArrayList<Long> courseIds);

    @Select("select * from course where teacher_id = #{teacherId} limit #{pageNum}, #{pageSize}")
    ArrayList<Course> getCourseListByTeacher(@Param("teacherId") Long teacherId,
                                             @Param("pageNum") Integer pageNum,
                                             @Param("pageSize") Integer pageSize);

    @Select("select count(*) from course where teacher_id = #{teacherId}")
    long getCourseTotalByTeacher(@Param("teacherId") Long teacherId);

    @Update("update course set final_exam_id = #{finalExamId} where id = #{courseId}")
    void setFinalExam(@Param("finalExamId") Long examId,
                      @Param("courseId")Long courseId);
}
